package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import model.OfferedCourse;
import model.Teacher;
import persistency.controller.OfferedCourseController;
import persistency.controller.TeacherController;
import controller.PlanningController;
import controller.StartupController;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TaskFrame extends JFrame {
	/* Required for serializable interface */
	private static final long serialVersionUID = 1L;
	/* The controller for this view */
	private PlanningController controller = PlanningController.getInstance();
	/* List of teachers */
	private JComboBox<Teacher> cboTeacher;
	/* Button to request tasks for given teacher */
	private JButton btnGetTasks;
	private JButton jButtonCancel;
	private JLabel jLabelMessage;
	private JButton jButtonClose;
	/* Button to request tasks for all teachers */
	private JButton btnGetTasksAll;
	/* Table to display the tasks*/
	private JTable tblTasks;
	private StartupController startupController;
	
	/** Constructs a new task frame */
	public TaskFrame(StartupController startupcontroller) {
		this.setSize(800, 600);
		this.setLayout(new BorderLayout());
		this.init();
		this.loadTeachers();
		this.getTasks(null);
		this.startupController = startupcontroller;
	}
	
	/* Builds  the frame */
	private void init() {
		// Get content pane
		Container pane = this.getContentPane();
		
		// pnStudent
		JPanel pnTeacher = new JPanel(new FlowLayout());
		
		// cboStudent
		this.cboTeacher = new JComboBox<Teacher>();
		pnTeacher.add(this.cboTeacher);
		
		// btnGetSchedule
		this.btnGetTasks = new JButton("Get");
		this.btnGetTasks.addActionListener(new BtnGetTasksClickedListener());
		pnTeacher.add(this.btnGetTasks);
		
		// btnGetScheduleAll
		this.btnGetTasksAll = new JButton("Get all");
		this.btnGetTasksAll.addActionListener(new BtnGetTasksAllClickedListener());
		pnTeacher.add(this.btnGetTasksAll);
		/*{
			jButtonClose = new JButton();
			pnTeacher.add(jButtonClose);
			jButtonClose.setText("Close");
			jButtonClose.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					Object offeredCourseId  = null;
					try {
						try {
							offeredCourseId = tblTasks.getModel().getValueAt(tblTasks.getSelectedRow(), 5);
						} catch (Exception e) {
							throw new Exception("No value selected!");
						}
						controller.closeOfferedCourseRegistrationByID(offeredCourseId);
						tblTasks.repaint();
						jLabelMessage.setForeground(Color.GREEN);
						jLabelMessage.setText("OfferedCourse registration closed succesfully!");
					} catch (Exception e) {
						jLabelMessage.setForeground(Color.RED);
						jLabelMessage.setText("Failed to close registration for OfferedCourse " + e.getMessage());
						e.printStackTrace();
					}
				}
			});
		}*/
		/*{
			jButtonCancel = new JButton();
			pnTeacher.add(jButtonCancel);
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					Object offeredCourseId  = null;
					try {
						try {
							offeredCourseId = tblTasks.getModel().getValueAt(tblTasks.getSelectedRow(), 5);
						} catch (Exception e) {
							throw new Exception("No value selected!");
						}
						controller.cancelOfferedCourseRegistrationByID(offeredCourseId);
						tblTasks.repaint();
						jLabelMessage.setForeground(Color.GREEN);
						jLabelMessage.setText("OfferedCourse canceled succesfully!");
					} catch (Exception e) {
						jLabelMessage.setForeground(Color.RED);
						jLabelMessage.setText("Failed to cancel OfferedCourse " + e.getMessage());
						e.printStackTrace();
					}
				}
			});
		}*/

		// tblSchedule
		this.tblTasks = new JTable();
		tblTasks.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		JScrollPane scrollPane = new JScrollPane(tblTasks);
		
		// Add components to frame
		pane.add(pnTeacher, BorderLayout.NORTH);
		pane.add(scrollPane, BorderLayout.SOUTH);
		{
			jLabelMessage = new JLabel();
			pane.add(jLabelMessage, BorderLayout.CENTER);
		}
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				showMenu();
			}
		});
	}
	
	/* Gets the tasks for a teacher or for all teachers */
	private void getTasks(Teacher teacher) {
		Collection<OfferedCourse> tasks;
		
		// Get tasks
		if (teacher == null) {
			tasks = OfferedCourseController.instance().getAll();
		} else {
			tasks = OfferedCourseController.instance().getByTeacherIdOfferedCourses(teacher.getTeacherCode()); 
		}
				
		// Set model for table
		TaskModel taskModel = new TaskModel(tasks);
		this.tblTasks.setModel(taskModel);
	}
	
	/* Loads all teachers into the combo box */
	private void loadTeachers() {
		// Clear the combobox
		this.cboTeacher.removeAllItems();
		// Request a list of teachers
		ArrayList<Teacher> teachers = (ArrayList<Teacher>)TeacherController.instance().getAll();
		// Add to combobox
		for (Teacher teacher : teachers) {
			this.cboTeacher.addItem(teacher);
		}
	}
	
	/** Get button click handler */
	public void BtnGetTasksClickedHandler(ActionEvent e){
		Teacher teacher = (Teacher)this.cboTeacher.getSelectedItem();
		this.getTasks(teacher);
	}
	
	/** Get button click handler */
	public void BtnGetTasksAllClickedHandler(ActionEvent e){
		this.getTasks(null);
	}
	
	/** Get button click listener */
	private class BtnGetTasksClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetTasksClickedHandler(e);
		}
	}
	
	/** Get button click listener */
	private class BtnGetTasksAllClickedListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			BtnGetTasksAllClickedHandler(e);
		}
	}
	
	private void showMenu() {
		startupController.execute();
	}
}